﻿#include "schemedialog.h"
#include "ui_schemedialog.h"

#include<QDebug>
namespace{
    enum EMountCol{
                MountID=0,      //方案ID
                MountSum,       //挂载总量
                MountConfig     //挂载配置
    };
    QVariantMap idToNameMap;    //通过装备ID获取装备名称
}
SchemeDialog::SchemeDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::SchemeDialog)
{
    ui->setupUi(this);
}

SchemeDialog::SchemeDialog(const QString &name, const QList<FATable> currNameInfos, QWidget *parent)
    :QDialog(parent),
    ui(new Ui::SchemeDialog),
    m_name(name),
    m_currNameInfos(currNameInfos)
{
    ui->setupUi(this);
    initUI();
    updataTabel();
}

SchemeDialog::~SchemeDialog()
{
    delete ui;
}

void SchemeDialog::initUI()
{
    setWindowFlags(Qt::FramelessWindowHint);  // 设置无边框样式
    ui->tableWidget->setEditTriggers(QAbstractItemView::NoEditTriggers);
    ui->tableWidget->horizontalHeader()->setSelectionBehavior(QAbstractItemView::SelectRows);   // 选择行为：行
    ui->tableWidget->horizontalHeader()->setSelectionMode(QAbstractItemView::SingleSelection);  // 选择模式：单选
    ui->tableWidget->horizontalHeader()->setStretchLastSection(true);
    ui->tableWidget->verticalHeader()->setVisible(false);

    idToNameMap.clear();
    QVariantMap idToClassifyMap;
    for (auto &var:nTable) {
        idToNameMap.insert(var.nameId,var.name);
        idToClassifyMap.insert(var.nameId,var.classify);
    }
    ui->nameLabel->setText(idToNameMap.value(m_name).toString());

    QMap<int,QString> idKeyMap;
    for (auto &var:cTable) {
        idKeyMap.insert(var.classify,var.className);
    }
    ui->typeLabel->setText(idKeyMap.value(idToClassifyMap[m_name].toInt()));
}

void SchemeDialog::updataTabel()
{
    ui->tableWidget->setRowCount(0);
    QMap<int,int> mountSumNumMap;
    QMap<int,QMap<QString,int>> mountNumMap;
    for (auto &it:m_currNameInfos) {
        mountSumNumMap[it.idFA]+=it.equipNum;
        mountNumMap[it.idFA][it.equipId]=it.equipNum;
    }

    for (auto &faId:mountSumNumMap.keys()) {
        int row=ui->tableWidget->rowCount();
        ui->tableWidget->insertRow(row);
        ui->tableWidget->setItem(row,EMountCol::MountID,new QTableWidgetItem(QString::number(faId)));
        ui->tableWidget->setItem(row,EMountCol::MountSum,new QTableWidgetItem(QString::number(mountSumNumMap[faId])));
        QStringList mountConfig;
        for (auto &equipId:mountNumMap[faId].keys()) {
            mountConfig.append(QString("%1(%2个)").arg(idToNameMap[equipId].toString()).arg(mountNumMap[faId][equipId]));
        }
        ui->tableWidget->setItem(row,EMountCol::MountConfig,\
                                new QTableWidgetItem(mountConfig.join("+")));
    }
}

void SchemeDialog::updataFoldBtnIcon(bool isFold)
{
    if(isFold)
        ui->foldBtn->setText("□");
    else
        ui->foldBtn->setText("-");
}

void SchemeDialog::setFoldView(bool isFold)
{
    ui->groupBox->setVisible(!isFold);
    updataFoldBtnIcon(isFold);
}

void SchemeDialog::on_foldBtn_clicked()
{
    bool isFold = ui->groupBox->isVisible();
    ui->groupBox->setVisible(!isFold);
    emit isFoldSignal(isFold);
    updataFoldBtnIcon(isFold);
}

